/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager.edit;

import br.com.foxline.model.Address;
import br.com.foxline.model.City;
import br.com.foxline.model.Corrector;
import br.com.foxline.service.CityService;
import br.com.foxline.service.CorrectorService;
import br.com.foxline.util.Msg;
import java.io.Serializable;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
//import org.lavieri.modelutil.cep.WebServiceCep;

/**
 *
 * @author luciano
 */
@ManagedBean
@ViewScoped
public class ManagerEditCorrector implements Serializable {

    private Corrector corrector;
    private Address address;
    private City city;
    @EJB
    private CorrectorService correctorService;
    @EJB
    private CityService cityService;

    @PostConstruct
    public void init() {
        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String correctorId = params.get("correctorId");
        corrector = correctorService.find(Long.valueOf(correctorId));
        if (corrector.getAddress() == null) {
            corrector.setAddress(new Address());
        }
    }

    public void save() {
        try {
            correctorService.update(corrector);
            if (corrector.getAddress().getCity() != null) {
                corrector.getAddress().setCity(cityService.find(corrector.getAddress().getCity().getId()));
            }
            Msg.messagemInfo(Msg.SuccessFull);
        } catch (Exception e) {
            Msg.messagemError(Msg.Failure);
        }
    }

//        public void searchCep() {
//        WebServiceCep webServiceCep = WebServiceCep.searchCep(address.getCep());
//        address.setPatio(webServiceCep.getLogradouroFull());
//        address.setDistrict(webServiceCep.getBairro());
//        String c = webServiceCep.getCidade();
//        try {
//            city = cityService.autocomplete(c).get(0);
//        } catch (Exception erro) {
//            Msg.messagemInfo("Cidade não possue no nosso banco de Informação");
//        }
//    }
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public Corrector getCorrector() {
        return corrector;
    }

    public void setCorrector(Corrector corrector) {
        this.corrector = corrector;
    }
}
